package com;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IO {//报销人员信息写入
    public static void writeBX(List<BXEmployee> bxEmployees){
        try {
            FileWriter fw = new FileWriter("报销人员.txt");
            Iterator<BXEmployee> iterator = bxEmployees.iterator();
            StringBuilder data = new StringBuilder();
            for (BXEmployee bx : bxEmployees) {
                data.append(bx.getId()).append(" ").append(bx.getName()).append(" ").append(bx.getPhone()).append("\n");
            }
            fw.write(data.toString());//一行一行的写入
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //审批人员信息写入
    public static void writeSP(List<SPEployee> spEployees){
        try {
            FileWriter fw = new FileWriter("审批人员.txt");
            Iterator<SPEployee> iterator = spEployees.iterator();
            StringBuilder data = new StringBuilder();
            for (SPEployee sp : spEployees) {
                data.append(sp.getId()).append(" ").append(sp.getName()).append(" ").append(sp.getPhone()).append(" ").append(sp.getRole()).append("\n");
            }
            fw.write(data.toString());//一行一行的写入
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //报销审核流程信息写入
    public static void writeProcess(List<Process> processes){
        try {
            FileWriter fw = new FileWriter("报销流程.txt");
            Iterator<Process> iterator = processes.iterator();
            StringBuilder data = new StringBuilder();
            for (Process process : processes) {
                data.append(process.getId()).append(" ").append(process.getBxEp_id()).append(" ").append(process.getMoney()).append(" ").append(process.getDate()).append(" ").append(process.getStatus()).append(" ").append(process.getProcess_id()).append("\n");
            }
            fw.write(data.toString());//一行一行的写入
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //授权额度信息写入
    public static void writeEDSQ(List<SQED> SQEDList){
        try {
            FileWriter fw = new FileWriter("授权额度.txt");
            Iterator<SQED> iterator = SQEDList.iterator();
            StringBuilder data = new StringBuilder();
            for (SQED SQED : SQEDList) {
                data.append(SQED.getRole()).append(" ").append(SQED.getLimit()).append(" ").append(SQED.getProcess_id()).append("\n");
            }
            fw.write(data.toString());//一行一行的写入
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //从文件中读取报销人员数据
    public static List<BXEmployee> readBX(){
        List<BXEmployee> list = new ArrayList<>();
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader("报销人员.txt");
            br = new BufferedReader(fr);
            String line = "";
            line = br.readLine();//一行一行的读入
            while (line != null) {
                String[] info = line.split(" ");
                list.add(new BXEmployee(info[0],info[1],info[2]));
                line = br.readLine();
            }
        } catch (IOException e) {//捕捉异常
            e.printStackTrace();
        }finally {
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }return list;
    }
    //从文件中读取授权额度数据
    public static List<SQED> readEDSQ(){
        List<SQED> list = new ArrayList<>();
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader("授权额度.txt");
            br = new BufferedReader(fr);
            String line = "";
            line = br.readLine();//一行一行的读入
            while (line != null) {
                String[] info = line.split(" ");
                list.add(new SQED(info[0],Integer.parseInt(info[1]),info[2]));
                line = br.readLine();
            }
        } catch (IOException e) {//捕捉异常
            e.printStackTrace();
        }finally {
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }return list;
    }
    //从文件中读取审批流程数据
    public static List<Process> readProcess(){
        List<Process> list = new ArrayList<>();
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader("报销流程.txt");
            br = new BufferedReader(fr);
            String line = "";
            line = br.readLine();//一行一行的读入
            while (line != null) {
                String[] info = line.split(" ");
                if (info.length == 5) {
                    list.add(new Process(info[0],info[1],Integer.parseInt(info[2]),info[3],info[4]));
                } else {
                    list.add(new Process(info[0],info[1],Integer.parseInt(info[2]),info[3],info[4],info[5]));
                }
                line = br.readLine();
            }
        } catch (IOException e) {//捕捉异常
            e.printStackTrace();
        }finally {
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }return list;
    }
    //从文件中读取审批人员数据
    public static List<SPEployee> readSP(){
        List<SPEployee> list = new ArrayList<>();
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader("审批人员.txt");
            br = new BufferedReader(fr);
            String line = "";
            line = br.readLine();//一行一行的读入
            while (line != null) {
                String[] info = line.split(" ");
                list.add(new SPEployee(info[0],info[1],info[2],info[3]));
                line = br.readLine();
            }
        } catch (IOException e) {//捕捉异常
            e.printStackTrace();
        }finally {
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }return list;
    }
}
